package org.firebug.spring.boot.starter.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping(value = {"/session"})
public class SessionController {
    @RequestMapping(value = {"/init"}, method = {RequestMethod.GET})
    public ResponseEntity<String> init(HttpServletRequest request) {
        return ResponseEntity.ok(request.getSession().getId());
    }

    @RequestMapping(value = {"/set"}, method = {RequestMethod.GET})
    public ResponseEntity<String> set(HttpSession session, @RequestParam String k, @RequestParam String v) {
        session.setAttribute(k, v);
        return ResponseEntity.ok(session.getId());
    }

    @RequestMapping(value = {"/get"}, method = {RequestMethod.GET})
    public ResponseEntity<String> get(HttpSession session, @RequestParam String k) {
        return ResponseEntity.ok((String)session.getAttribute(k));
    }
}